
$(window).on('load', function () {
    Init();
});
var companyNameStatus = false;//默认企业名称为假，不可查询
/**
*页面初始化
* */
var Init = function () {
    //affix滚动导航
    $('#nav').affix({});
    $('body').scrollspy({ target: '.scrollspy' })
    $('#datetimepicker').datepicker({
        format: 'yyyy',
        weekStart: 1,
        autoclose: true,
        startView:'years',
        minViewMode:'years',
        language: 'zh-CN'
    });

    $('#companyNameListText').on('input propertychange', function() {
        var name = $(this).val();
        companyNameStatus = false;
        getCompanyNameList(name)
    });

    $("#page-search").on('click',function () {
        getSearchFunc();
    })

    checkHasUrlQuery();
}
/**
 * 检查是否有code参数
 */
var checkHasUrlQuery = function () {
    var code = getQueryString("code");
    if(code){
        getCompanyBaseInfoByCode(code);
    }
}
/**
 * 获取URL后的参数
 */
var getQueryString = function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
/**
 * 根据用户输入名称去模糊查询企业列表
 */
var getCompanyNameList = function (name) {
    SGIS.API.post("/enterprise/get/name/list").data({"enterpriseName":name}).json(function (re) {
        if(re.length>0){
            renderCompanyNameList(re);
        }else{
            renderCompanyNameList();
        }
    });
}
/**
 * 渲染组装企业列表
 */
var renderCompanyNameList = function (data) {
    var arr = [];
    if(data){
        arr = data;
    }
    var html = '';
    for(var i = 0 ; i < arr.length;i++){
        var btn = '<button type="button" class="list-group-item" >'+arr[i].a00002||''+'</button>';
        html +=btn;
    }
    $(".companyNameListGroup").css('display','inline-block');
    $(".companyNameListGroup").html(html);
    InitSelectFunc();
}
/**
 * 初始化企业列表选择事件
 */
var InitSelectFunc = function () {
    $(".companyNameListGroup button").on('click',function () {
        var value = $(this).text();
        $("#companyNameListText").val(value);
        $(".companyNameListGroup").css('display','none');
        companyNameStatus = true;
    });
}
/**
 * 执行页面搜索查询事件
 */
var getSearchFunc = function () {
    var companyName = $("#companyNameListText").val();
    var reportTime = $("#datetimepicker").val();
    if(!companyNameStatus){
        alert("请输入正确的企业名称后再查询");
    }else{
        getCompanyBaseInfo(companyName);
    }
}



var setBaseInformiation = function () {
    var html = "<tr>" +
        "<th>单位详细名称</th>" +
        "<td id='danwei'></td>" +
        "<th>组织机构代码</th>" +
        "<td id='zuzhi'></td>" +
        "</tr>" +
        "<tr>" +
        "<th>营业状态</th>" +
        "<td id='yingye'></td>" +
        "<th>登记注册类型</th>" +
        "<td id='dengji'></td>" +
        "</tr>" +
        "<tr>" +
        "<th>注册地址</th>" +
        "<td id='zhucedizhi'></td>" +
        "<th>法人名称</th>" +
        "<td id='fading'></td>" +
        "</tr>" +
        "<tr>" +
        "<th>成立日期</th>" +
        "<td id='chengli'></td>" +
        "<th>注册或登记号</th>" +
        "<td id='dengjihao'></td>" +
        "</tr>";


    $("#baseInformation").html(html);
}



/**
 *根据企业详细名称获取企业统计信息（基本信息）
 */
var getCompanyBaseInfo = function (enterpriseName) {
    $("#baseInformation").html();
    SGIS.API.post("/enterprise/get/name/info").data({'enterpriseName':enterpriseName}).json(function (re) {
        setBaseInformiation();
        if(re.hasOwnProperty("code")){
            var arr = [];
            var code = re.code;
            var value = re.value;
            for(var i in code){
                arr.push({
                    "code":code[i]||'',
                    "value":value[i]||''
                })
            }
            renderTableData(arr,2,"#baseInformation");
            renderTableData2(arr,2,"#baseInfo-tjxx");
        }else{
            return;
        }
    });
}



/**
 *根据组织机构代码获取企业统计信息（基本信息）
 */
var getCompanyBaseInfoByCode = function (code) {
    $("#baseInformation").html();
    SGIS.API.post("/enterprise/get/code/info").data({'code':code}).json(function (re) {
        setBaseInformiation();
        if(re.hasOwnProperty("code")){
            var arr = [];
            var code = re.code;
            var value = re.value;
            for(var i in code){
                arr.push({
                    "code":code[i]||'',
                    "value":value[i]||''
                })
            }
            var companyName = $("#companyNameListText").val();
            if(!companyName){
                $("#companyNameListText").val(value.A00002)
            }
            renderTableData(arr,2,"#baseInformation");
            renderTableData2(arr,2,"#baseInfo-tjxx");
        }else{
            return;
        }
    });
}
/**
 *基本信息表格数据渲染
 */
var renderTableData = function (data,colnum,element) {

    var len = data.length;
    var rownum = len/colnum;
    var html = '';


    $("#danwei").text(data[9].value);
    $("#zuzhi").text(data[5].value);
    $("#zhucedizhi").text(data[11].value);
    for(var i = 0; i < rownum; i++){
        var tr = '';
        tr = $(element).append('<tr class="infotable"><th class="infotableth">'+data[2*i].code+'</th><td>'+data[2*i].value+'</td><th>'+data[2*i+1].code+'</th><td>'+data[2*i+1].value +'</td></tr>');
        html+=tr;
    }
    //$(element).html(html);
}

var renderTableData2 = function (data,colnum,element) {
    var len = data.length;
    var rownum = len/colnum;
    var html = '';


    for(var i = 0; i < rownum; i++){
        var tr = '';
        tr = '<tr><th>'+data[2*i].code+'</th><td>'+data[2*i].value+'</td><th>'+data[2*i+1].code+'</th><td>'+data[2*i+1].value +'</td></tr>';
        html+=tr;
    }
    $(element).empty();
    $(element).html(html);
}